<template>
  <div class="page">
    <div class="title">
      <div class="h-title">NLP自然语言免费翻译</div>
      <div class="sub-title">基于Helsinki-NLP</div>
      <div class="sub-title">
        毕竟是AI翻译，偶尔抽风也很正常，希望别怪罪我，可以去找这个预训练模型的作者->
        <a href="https://huggingface.co/Helsinki-NLP" target="_blank">
          Helsinki-NLP
        </a>
      </div>
    </div>
    <div>
      <div class="input-content">
        <el-input
          :autosize="{ minRows: 6, maxRows: 30 }"
          v-model="content"
          placeholder="请输入要翻译的语句"
          type="textarea"
        ></el-input>
      </div>
    </div>
    <div class="opera">
      <el-select v-model="type" placeholder="请选择类型" style="width: 150px">
        <el-option label="中文->英文" value="zh-en"></el-option>
        <el-option label="英文->中文" value="en-zh"></el-option>
      </el-select>
      <el-button type="primary" @click="handleTranslation">翻译</el-button>
    </div>
    <div v-loading="loading">
      <div class="input-content">
        <el-input
          :autosize="{ minRows: 6, maxRows: 30 }"
          v-model="traslationResult"
          placeholder="等待服务器响应...."
          type="textarea"
        ></el-input>
      </div>
      <i
        class="copy"
        :class="
          copyStatus ? 'el-icon-document-checked' : 'el-icon-document-copy'
        "
        @click="copy"
      ></i>
    </div>
    <div class="footer">
      <a href="http://www.beian.miit.gov.cn" target="_blank"
        >粤ICP备20009502号</a
      >
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "index",
  data() {
    return {
      content: "",
      type: "zh-en",
      traslationResult: "",
      copyStatus: false,
      loading: false,
    };
  },
  methods: {
    handleTranslation() {
      axios.defaults.timeout = 30000;
      this.copyStatus = false;
      this.loading = true;
      axios
        .get(
          `http://43.139.27.164:2222/translation/${this.type}?content=${this.content}`
          // `http://192.168.1.11:2222/translation/${this.type}?content=${this.content}`
        )
        .then((ret) => {
          this.traslationResult = ret.data.data;
          this.loading = false;
        })
        .catch(() => {
          this.$notify.error("翻译失败");
          this.loading = false;
        });
    },
    copy() {
      if (this.$java) {
        this.$java.copy(this.traslationResult);
      } else {
        navigator.clipboard.writeText(this.traslationResult);
      }
      this.copyStatus = true;
      this.$message({
        message: "复制成功",
        type: "success",
      });
    },
  },
};
</script>
<style lang="less" scoped>
.copy {
  width: 100%;
  text-align: center;
  font-size: 20px;
  margin-top: 20px;
}
.opera {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.title {
  width: 80vw;
  margin: 0 auto;
  text-align: center;
  padding: 10px 0px 30px 0px;
  .h-title {
    font-size: 20px;
    padding-bottom: 10px;
    color: #303133;
    font-weight: 600;
  }
  .sub-title {
    font-size: 12px;
    color: #909399;
    padding-bottom: 10px;
  }
}
.footer {
  width: 100vw;
  margin: 0 auto;
  font-size: 12px;
  position: absolute;
  bottom: 30px;
  display: flex;
  align-items: center;
  justify-content: space-around;
}
</style>
